package com.example.demo.controller;

import com.example.demo.entity.Nav;
import com.example.demo.service.INavService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class NavController {

    @Autowired
    private INavService navService;

    @RequestMapping("/navList")
    public String navList(Model model){
        List<Nav> navList = navService.getAll();
        model.addAttribute("navList", navList);
        return "admin/nav/navList";
    }

    @RequestMapping(value="/toEditNav")
    public String toEdit(Model model,Integer id){

        if(id!=null){

            Nav nav = navService.selectById(id);

            model.addAttribute("nav", nav);
        }

        return "admin/nav/nav";

    }

    @RequestMapping(value="/editNav")
    public String edit(Nav nav){

        if(nav.getId()!=null){
            navService.update(nav);
        }else{
            navService.insert(nav);
        }

        return "redirect:navList";
    }

    @RequestMapping(value="/deleteNav")
    public String delete(Integer id){

        navService.delete(id);

        return "redirect:navList";

    }
}
